home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / lzw4w10.zip / RUNLZW.C < prev    next >
Text File  |  1994-07-12  |  5KB  |  212 lines

  1. /* runlzw.c */
  2.  
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "runlzw.h"
  7. #include "rw_io.h"
  8. #include "lzw4w.h"
  9. #include "display.h"
  10. #include "databox.h"
  11. #include "about.h"
  12. #include "sayerror.h"
  13.  
  14. /*
  15. ** PostMainHandle() is required only for the
  16. ** Shareware version of LZW4W.
  17. */
  18.  
  19. #if __cplusplus
  20. extern "C" void FAR PASCAL PostMainHandle(HWND);
  21. #else
  22. extern void FAR PASCAL PostMainHandle(HWND);
  23. #endif
  24.  
  25. static FARPROC lpProcDataBox;
  26. static FARPROC lpProcAbout;
  27. static HANDLE  ghInstance;
  28.  
  29. static char InputFile[MAXEDITTEXT];
  30. static char OutputFile[MAXEDITTEXT];
  31. static char FileName[15];
  32.  
  33. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  34.           LPSTR lpszCmdParam, int nCmdShow)
  35. {
  36.  MSG Message;
  37.  HWND hWnd;
  38.  WNDCLASS WndClass;
  39.  
  40.  ghInstance = hInstance;
  41.  
  42.  if(!hPrevInstance)
  43.    {
  44.     WndClass.cbClsExtra = 0;
  45.     WndClass.cbWndExtra = 0;
  46.     WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  47.     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  48.     WndClass.hIcon = LoadIcon(hInstance, "LZWIcon");
  49.     WndClass.hInstance = hInstance;
  50.     WndClass.lpfnWndProc = WndProc;
  51.     WndClass.lpszClassName = "RUNLZW";
  52.     WndClass.lpszMenuName = "LZWMenu";
  53.     WndClass.style = CS_HREDRAW | CS_VREDRAW;
  54.     RegisterClass(&WndClass);
  55.   }
  56.  
  57.  hWnd = CreateWindow("RUNLZW",  "RUNLZW", WS_OVERLAPPEDWINDOW,
  58.           CW_USEDEFAULT, CW_USEDEFAULT,
  59.           8*NCOLS,       12*NROWS+48,
  60.           NULL,          NULL,
  61.           hInstance,     NULL);
  62.  /* show time ! */
  63.  ShowWindow(hWnd, nCmdShow);
  64.  while( GetMessage(&Message,NULL,0,0))
  65.    {TranslateMessage(&Message);
  66.     DispatchMessage(&Message);
  67.    }
  68.  return Message.wParam;
  69. }
  70.  
  71.  
  72. static CheckError(HWND hWnd,int Code)
  73. {if(Code<0)
  74.   {SayError(hWnd,Code);
  75.    return FALSE;
  76.   }
  77.  else return TRUE;
  78. }
  79.  
  80. static void CopyNameOnly(LPSTR Dest,LPSTR Source)
  81. {int i = 0;
  82.  char c;
  83.  while(1)
  84.    {c = (char) *Source++;
  85.     if((c==':')||(c=='\\')) i = 0;
  86.     else if(c=='.') break;
  87.     else Dest[i++] = c;
  88.    }
  89.  Dest[i] = '\0';
  90. }
  91.  
  92. /*
  93. ** Prompts user for input & output file names
  94. ** and opens the Reader & Writer
  95. */
  96.  
  97. static int GetFileNames(HWND hWnd,LPSTR InputPrompt,LPSTR OutputPrompt)
  98. {/* get input file name */
  99.  SetDlgInfoString(InputPrompt);
  100.  if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) return FALSE;
  101.  lstrcpy(InputFile,GetDlgTextPtr());
  102.  AnsiUpper(InputFile);
  103.  if(!ReaderOpen(InputFile))
  104.    {MessageBox(hWnd,InputFile,"Cannot open",MB_ICONEXCLAMATION);
  105.     return FALSE;
  106.    }
  107.  /* get output file name */
  108.  SetDlgInfoString(OutputPrompt);
  109.  if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) return FALSE;
  110.  lstrcpy(OutputFile,GetDlgTextPtr());
  111.  AnsiUpper(OutputFile);
  112.  if(!WriterOpen(OutputFile))
  113.    {MessageBox(hWnd,OutputFile,"Cannot open",MB_ICONEXCLAMATION);
  114.     return FALSE;
  115.    }
  116.  /* display base part of filename */
  117.  CopyNameOnly(FileName,InputFile);
  118.  DisplayText(FileName);
  119.  return TRUE;
  120. }
  121.  
  122. /********** WndProc ************/
  123.  
  124. long FAR PASCAL WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
  125. {int i;
  126.  int Code;
  127.  HDC hDC;
  128.  HPEN hPen;
  129.  PAINTSTRUCT ps;
  130.  float X, Y;
  131.  float V;
  132.  POINT Point;
  133.  switch(iMessage)
  134.    {
  135.     case WM_PAINT:
  136.  
  137.       hDC = BeginPaint(hWnd, &ps);
  138.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  139.       DisplayPaint(hDC,&ps);
  140.       EndPaint(hWnd,&ps);
  141.       break;
  142.  
  143.     case WM_COMMAND:
  144.  
  145.       switch(wParam)
  146.          {case MSG_ABOUT:
  147.             DialogBox(ghInstance,"AboutBox",hWnd,lpProcAbout);
  148.             break;
  149.  
  150.           case MSG_COMPRESS:
  151.  
  152.             if(!GetFileNames(hWnd,"Enter file to compress",
  153.                                   "Name of compressed file")) break;
  154.             /* compress */
  155.             if(!CheckError(hWnd,InitLZW(14))) break;
  156.             if(!CheckError(hWnd,Compress(Reader,Writer))) break;
  157.             if(!CheckError(hWnd,TermLZW())) break;
  158.             ReaderClose();
  159.             WriterClose();
  160.             DisplayText(" OK\r\n");
  161.             break;
  162.  
  163.           case MSG_EXPAND:
  164.  
  165.             if(!GetFileNames(hWnd,"Enter file to expand",
  166.                                   "Name of expanded file")) break;
  167.             if(!CheckError(hWnd,InitLZW(14))) break;
  168.             if(!CheckError(hWnd,Expand(Reader,Writer))) break;
  169.             if(!CheckError(hWnd,TermLZW())) break;
  170.             ReaderClose();
  171.             WriterClose();
  172.             DisplayText(" OK\r\n");
  173.             break;
  174.  
  175.           case MSG_EXIT:
  176.  
  177.             PostQuitMessage(0);
  178.             break;
  179.          }
  180.       break;
  181.  
  182.     case WM_CREATE:
  183.  
  184.       /* create AboutDlgProc() thunk */
  185.       lpProcAbout = MakeProcInstance((FARPROC)AboutDlgProc,ghInstance);
  186.       /* create DataBoxDlgProc() thunk */
  187.       lpProcDataBox = MakeProcInstance((FARPROC)DataBoxDlgProc,ghInstance);
  188.       /* initialize window display */
  189.       DisplayInit(hWnd);
  190.       /*
  191.       ** You must call PostMainHandle() before attemping to call LZW.
  192.       ** This is required only for the Shareware version of LZW4W.
  193.       */
  194.       PostMainHandle(hWnd);
  195.       if(!CheckError(hWnd,InitLZW(14)))
  196.         {PostQuitMessage(0);
  197.          break;
  198.         }
  199.       TermLZW();
  200.       break;
  201.  
  202.     case WM_DESTROY:
  203.  
  204.       PostQuitMessage(0);
  205.       break;
  206.  
  207.     default:
  208.  
  209.       return( DefWindowProc(hWnd,iMessage,wParam,lParam) );
  210.    }
  211.  return(NULL);
  212. }